<template>
  <teleport to="#bgLoading">
    <div
      :style="{ backgroundColor: background || '' }"
      class="d-flex justify-content-center align-items-center loading-container"
    >
      <div class="loading-content">
        <div class="spinner-border text-info" role="status">
          <span class="visually-hidden">Loading...</span>
        </div>
        <p v-if="text" class="text-info">{{ text }}</p>
      </div>
    </div>
  </teleport>
</template>

<script lang="ts">
import store from '@/store'
import { defineComponent, onMounted } from 'vue'
import useDomCreate from '../hooks/useDomCreate'
import { emitter } from './Uploader.vue'
import createMessage from '../hooks/createMessage'

export default defineComponent({
  props: {
    text: {
      type: String
    },
    background: {
      type: String
    }
  },
  setup () {
    useDomCreate('bgLoading')
    onMounted(() => {
      emitter.on('tooBig', () => {
        store.commit('setLoading', false)
        createMessage('文件大小超过限制', 'error')
      })
    })
  }
})
</script>

<style scoped>
.loading-container {
  width: 100%;
  height: 100%;
  z-index: 100;
  position: fixed;
  top: 0;
  left: 0;
  background-color: rgba(255, 255, 255, 0.5);
}
.loading-content {
  text-align: center;
}
</style>
